package com.wocao.controller;

import com.wocao.entity.ReqBody;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysDept;
import com.wocao.service.SysDeptService;
import com.wocao.utils.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门表 前端控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 部门树
     */
    @GetMapping("/deptTree")
    public ResponseResult postTree(){
        List<SysDept> treeList = Tree.deptTree(sysDeptService.list());
        return ResponseResult.ok(treeList);
    }

    //通过角色id查询部门id
    @GetMapping("/selectDeptIdsByRoleId/{roleId}")
    public ResponseResult selectDeptIdsByRoleId(@PathVariable long roleId){
        List<Long> deptIds = sysDeptService.selectDeptIdsByRoleId(roleId);
        return ResponseResult.ok(deptIds);
    }

    //搜索查询
    @PostMapping("/selectDeptAll")
    public ResponseResult<SysDept> selectDeptAll(@RequestBody ReqBody reqBody){
        List<SysDept> sysDepts = sysDeptService.selectDeptAll(reqBody);
        return ResponseResult.ok(sysDepts);
    }

    //部门添加
    @PostMapping("/deptAdd")
    public ResponseResult deptAdd(@RequestBody SysDept dept){
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        sysDeptService.deptAdd(dept);
        return ResponseResult.ok("添加成功");
    }

    //部门修改
    @PutMapping("/deptEdit")
    public ResponseResult deptEdit(@RequestBody SysDept dept){
        LocalDateTime now = LocalDateTime.now();
        dept.setUpdateTime(now);
        sysDeptService.deptEdit(dept);
        return ResponseResult.ok("修改成功");
    }
    //删除部门
    @DeleteMapping("/deleteDept/{deptId}")
    public ResponseResult deleteDept(@PathVariable long deptId){
        sysDeptService.deleteDept(deptId);
        return ResponseResult.ok("删除成功！！！");
    }

    //判断当前部门有没有子部门
    @GetMapping("/selectChildDept/{deptId}")
    public ResponseResult selectChildDept(@PathVariable long deptId){
        String s = sysDeptService.selectChildDept(deptId);
        return ResponseResult.ok(s);
    }

    //判断当前部门的父部门是否启用
    @GetMapping("/selectFatherDept/{parentId}")
    public ResponseResult selectFatherDept(@PathVariable long parentId){
        String s = sysDeptService.selectFatherDept(parentId);
        return ResponseResult.ok(s);
    }


}
